<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>创建调查问卷</title>
  <link rel="stylesheet" href="css/bootstrap.min.css">
  <link rel="stylesheet" href="css/layui.css">
</head>

<body style="background-image: url(img/3.jpg);;background-size: cover;">
  <div class="container">
    <div class="row align-items-start">
      <div class="col">
      </div>
      <div class="col">
        <!-- <div class="alert alert-danger text-center invisible" role="alert" style="width: 30rem;">
          <strong>提示：</strong>
          <font id="msgId"></font> -->
      </div>
    </div>
    <div class="col">

    </div>
  </div>
  <div class="row align-items-center">
    <div class="col">

    </div>
    <div class="col" style="opacity: 0.7;">
      <div class="shadow p-3 mb-5 bg-white rounded">
        <div class="">
          <div class="card-body">
            <div style="text-align: center;">

              <h1 style="text-align: center;">问卷标题:<label for="" id="title"></label></h1>



              <br>
              <br>
              <input type="button" class="button" value="+文本" style="float: right; width: 6rem; height: 2rem;"
                onclick="addByScript()">
              <br>
              <br>
              　<table id="tbls" style="margin: auto;">
              </table>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <br>
              <p style="float: right;">
                <button type="button" class="layui-btn" onclick="addtj()">保存</button>
                <button type="button" class="layui-btn" onclick="cancel()">取消</button>
              </p>
              <br>
              <br>
              <br>

            </div>


          </div>
        </div>
      </div>


    </div>
    <div class="col">

    </div>
  </div>

  <div class="row align-items-end">
    <div class="col">

    </div>
    <div class="col">

    </div>
    <div class="col">

    </div>
  </div>
  </div>



  <script src="js/jquery-3.6.0.min.js"></script>
  <script src="js/vue.js"></script>
  <script src="js/bootstrap.min.js"></script>
  <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
  <script src="js/layui.js"></script>
  <script>

    var count = 0;

    function addByScript() {
      var table = document.getElementById("tbls");
      var newRow = table.insertRow(table.rows.length);
      newRow.id = "row" + count;

      var contentCell = newRow.insertCell(-1);
      contentCell.innerHTML = '请输入问卷问题:<input type="text" name="username" placeholder="请输入问卷问题" style="width: 12rem;height: 2rem;margin-top: 2rem;" />';
      contentCell = newRow.insertCell(-1);

      var delBtn = document.createElement("input");
      delBtn.type = "button";
      delBtn.className = "button btn btn-primary ";
      delBtn.id = "btnDel" + count;
      delBtn.value = "删除";
      delBtn.style = "margin-top: 2rem;margin-left:1rem";
      delBtn.onclick = new Function("del(this)");
      contentCell.appendChild(delBtn);

      count++;
    }

    function del(obj) {
      var row = obj.parentNode.parentNode;
      row.parentNode.removeChild(row);
    }

    function addtj() {
      var arr = [];
      var names = document.getElementsByName("username");
      for (var i = 0; i < names.length; i++) {
        arr.push(names[i].value); //这个值就是你要的
      }

      var bool = arr.every(function (element) {
        return element.length > 0;
      });
      console.log(bool);
     
      if (bool==false) {

        alert("问卷问题不能为空")
      } else {


        axios({
          method: 'post',
          url: '/pronlem',
          data: {
            oneproblem: arr
          }
        }).then(function (response) {
          // axios工具下，服务器真正返回的数据在返回对象的data中
          let res = response.data;
          if (res.code === 200) {
            alert("提交成功");
            window.location.href = '/';
          }
        }).catch((err) => {

        });

      }
    }





    var s = decodeURI(document.location.href);
    var a = s.substring(s.indexOf('=') + 1);


    //取消
    function cancel() {
      window.location.href = '/'
    }


    document.getElementById('title').innerHTML = a;
  </script>
</body>

</html>